package com.zrong.project.pagination;

import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-7-2
 * Time: 上午11:31
 * To change this template use File | Settings | File Templates.
 */
@Component
public class OraclePaginationSQLGetter implements PaginationSQLGetter {
    public String getPaginationSQL(String originalSql, int first, int pageSize) {
        StringBuffer sql = new StringBuffer(" SELECT * FROM ( ");
        sql.append(" SELECT temp.* ,ROWNUM num FROM ( ");
        sql.append(originalSql);
        int last = first + pageSize;
        sql.append("　) temp where ROWNUM <= ").append(last);
        sql.append(" ) WHERE　num > ").append(first);
        return sql.toString();
    }

    @Override
    public String getCountSQL(String originalSql) {
        StringBuffer sql = new StringBuffer(" SELECT count(1) FROM ( ");
        sql.append(originalSql);
        sql.append(" )");
        originalSql.replace("*","count(1)");
        return sql.toString();

    }

}
